package com.adminx.dao;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.adminx.entity.Resource;
import com.adminx.pojo.NavigationMenuPojo;
import com.adminx.pojo.ResourcePojo;
import com.adminx.util.PageResults;

@Repository
public class ResourceDao extends BaseDao<Resource, Long>{  
	
	
	protected static Logger LOGGER = Logger.getLogger(ResourceDao.class);
	
	public PageResults<ResourcePojo> query(Integer pageIndex, Integer pageSize, String searchText, Long parentId){
		String sql = 
			"SELECT " +
			"	t.resource_id id, " +
			"	t.resource_name resourcesName, " +
			"	t.resource_path resourcesPath, " +
			"	t.parent_id parentID, " +
			"	DATE_FORMAT( " +
			"		t.create_date, " +
			"		'%Y-%m-%d %H:%i:%S' " +
			"	) createDate, " +
			"	DATE_FORMAT( " +
			"		t.update_date, " +
			"		'%Y-%m-%d %H:%i:%S' " +
			"	) updateDate " +
			"FROM " +
			"	xx_resources t where 1 = 1 ";
		
		String countSql = 
			"SELECT " +
			"	count(1) " +
			"FROM " +
			"	xx_resources t where 1 = 1 ";
		
		if(!StringUtils.isBlank(searchText)){
			sql += "and t.resource_name like '%" + searchText + "%' or t.remark like '%" + searchText + "%' ";
			countSql += "and t.resource_name like '%" + searchText + "%' or t.remark like '%" + searchText + "%' ";
		}
		
		if(null != parentId){
			sql += "and t.parent_id = " + parentId;
			countSql += "and t.parent_id = " + parentId;
		}else{
			sql += "and t.parent_id = 0 ";
			countSql += "and t.parent_id = 0 ";
		}
		
		pageIndex = pageIndex == null?1:pageIndex;
		pageSize = pageSize == null? 10:pageSize;
		
		return this.findPageByFetchedSql(ResourcePojo.class, sql, countSql, pageIndex, pageSize);
	}
	
	/**
	 * 通过id返回User
	 * @param id
	 * @return
	 */
	public Resource getResource(Long id){
		return this.get(id);
	}

	/**
	 * 通过ID删除账号
	 * @param accountId
	 */
	public void deleteByAccountId(Long accountId) {
		this.deleteById(accountId);
	}
	
	/**
	 * 批量删除
	 * @param roleID 角色ID
	 * @return 角色
	 */
	public int delBatch(String ids){
		String[] idsArray = ids.split(",");
		String hql = "";
		for (int i = 0; i < idsArray.length; i++) {
			if (i == 0) {
				hql = "resourceId=" + idsArray[i];
			} else {
				hql = hql + " or resourceId=" + idsArray[i];
			}
		}
		int updateCount = this.getSession().createQuery("DELETE FROM Resource WHERE " + hql).executeUpdate();
		return updateCount;
	}
	
	/**
	 * 查询导航数据
	 */
	public NavigationMenuPojo queryNavigation(String resourcePath){
		String sql = 
			"SELECT " +
			"	a.resource_id resourceId, " +
			"	a.resource_name resourceName, " +
			"	a.resource_path resourcePath, " +
			"	a.resource_icon resourceIcon, " +
			"	c.resource_id parentResourceId, " +
			"	c.resource_name parentResourceName, " +
			"	c.resource_path parentResourcePath, " +
			"	c.resource_icon parentResourceIcon " +
			"FROM " +
			"	xx_resources c, " +
			"	( " +
			"		SELECT " +
			"			t.resource_id, " +
			"			t.resource_name, " +
			"			t.resource_path, " +
			"			t.resource_icon, " +
			"			t.parent_id " +
			"		FROM " +
			"			xx_resources t " +
			"		WHERE " +
			"			t.resource_path = '"+resourcePath+"' LIMIT 1 " +
			"	) a " +
			"WHERE " +
			"	c.resource_id = a.parent_id";
		return (NavigationMenuPojo) this.getSession().createSQLQuery(sql).setResultTransformer(Transformers.aliasToBean(NavigationMenuPojo.class)).uniqueResult();
	}
}
